//package com.baoer.Filter;
//
//import com.alibaba.fastjson.JSON;
//import com.baoer.util.JwtUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.core.io.buffer.DataBuffer;
//import org.springframework.http.HttpStatus;
//import org.springframework.stereotype.Component;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.nio.charset.StandardCharsets;
//import java.util.HashMap;
//import java.util.Map;
//
//@Component
//public class AuthenticationFilter implements GlobalFilter, Ordered {
//
//    @Autowired
//    private JwtUtil jwtUtil;
//
//    @Override
//    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
//        System.out.println("1111111111111");
//        String path = exchange.getRequest().getPath().toString();
//        // 放行登录和其他不需要认证的接口
//        if (path.contains("/login") || path.contains("/images") || path.contains("/h5")) {
//            return chain.filter(exchange);
//        }
//
//        String token = exchange.getRequest().getHeaders().getFirst("token");
//        if(token == null||token.isEmpty()){
//            return unauthorizedResponse(exchange, "当前无token");
//        }
//        try {
//            Map<String, Object> map = jwtUtil.parseJWT(token);
//        } catch (Exception e) {
//            return unauthorizedResponse(exchange, "token转换错误：" + e.getClass());
//        }
//        if(!jwtUtil.verityToken(token)){
//            return unauthorizedResponse(exchange, "token已过期");
//        }
//        if(!jwtUtil.isTrue(token)) {
//            return unauthorizedResponse(exchange, "token不正确");
//        }
//        return chain.filter(exchange);
//    }
//
//    @Override
//    public int getOrder() {
//        return 0;
//    }
//
//    private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String message) {
//        exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
//        exchange.getResponse().getHeaders().add("Content-Type", "application/json");
//        Map<String, Object> response = new HashMap<>();
//        response.put("code", 401);
//        response.put("message", message);
//        DataBuffer buffer = exchange.getResponse().bufferFactory()
//                .wrap(JSON.toJSONString(response).getBytes(StandardCharsets.UTF_8));
//        return exchange.getResponse().writeWith(Mono.just(buffer));
//    }
//}
